from django.db import models
from dingxiang.utils.models import BaseModel
from AskDoctor.models import DoctorModel
from disease.models import DepartmentModel
from user.models import UserModel
from SearchHospital.models import HospitalModel
from orders.models import OrdersModel
# Create your models here.
class CouponModel(BaseModel):
    DISCOUNT_CHOICES = {
        (1,"减免"),
        (2,"折扣"),
    }
    TYPE_CHOICES = {
        (0,"通用类型"),
        (1,"指定课程方向"),
        (2,"指定课程分类"),
        (3,"指定课程"),
    }
    GET_CHOICES = {
        (0,"系统赠送"),
        (1,"用户主动领取")
    }
    discount = models.SmallIntegerField(choices=DISCOUNT_CHOICES,default=1,verbose_name="优惠方式")
    coupon_type = models.SmallIntegerField(choices=TYPE_CHOICES,default=0,verbose_name="优惠券类型")
    get_type = models.SmallIntegerField(choices=GET_CHOICES,default=0,verbose_name="优惠券领取方式")
    total = models.IntegerField(blank=True,null=True,default=100,verbose_name="发放数量")
    has_total = models.IntegerField(blank=True,null=True,default=100,verbose_name="剩余数量")
    start_time = models.DateTimeField(verbose_name="使用时间")
    end_time = models.DateTimeField(verbose_name="过期时间")
    condition = models.IntegerField(blank=True,default=0,verbose_name="满足优惠券使用的价格条件")
    per_limit = models.SmallIntegerField(default=1,verbose_name="每人限制领取的数量")
    sale = models.CharField(max_length=100,verbose_name="优惠公式",help_text="*表示打折,示例:*0.8表示打八折<br/>-表示满减,示例:-100表示总价上减去100")

    class Meta():
        db_table = 'fg_coupon'
        verbose_name_plural = "优惠券"


# 优惠券与医院
class CouponCourseDirectionModel(models.Model):
    coupon = models.ForeignKey(CouponModel,on_delete=models.CASCADE,related_name="to_direction",db_constraint=False,verbose_name="优惠券")
    direction = models.ForeignKey(HospitalModel,on_delete=models.CASCADE,related_name="to_coupon",db_constraint=False,verbose_name="专业方向")
    created_time = models.DateTimeField(auto_now_add=True,verbose_name="添加时间")
    class Meta():
        db_table = "fg_coupon_course_direction"


# 优惠券与科室
class CouponCourseCategoryModel(models.Model):
    coupon = models.ForeignKey(CouponModel,on_delete=models.CASCADE,related_name="to_category",db_constraint=False,verbose_name="优惠券")
    category = models.ForeignKey(DepartmentModel,on_delete=models.CASCADE,related_name="to_coupon",db_constraint=False,verbose_name="专业分类")
    created_time = models.DateTimeField(auto_now_add=True,verbose_name="添加时间")
    class Meta():
        db_table = "fg_coupon_course_category"
        verbose_name_plural = "优惠券与课程分类"


# 优惠券与医生
class CouponCourseModel(models.Model):
    coupon = models.ForeignKey(CouponModel,on_delete=models.CASCADE,related_name="to_course",db_constraint=False,verbose_name="优惠券")
    course = models.ForeignKey(DoctorModel,on_delete=models.CASCADE,related_name="to_coupon",db_constraint=False,verbose_name="课程")
    created_time = models.DateTimeField(auto_now_add=True,verbose_name="添加时间")
    class Meta():
        db_table = "fg_coupon_course"
        verbose_name_plural = "优惠券与课程信息"


# 优惠券管理类
class CouponLogModel(BaseModel):
    USE_CHOICES = {
        (0,"未使用"),
        (1,"已使用"),
        (2,"已过期"),
    }
    # 赠送给用户,   是否使用  使用的时间,      针对哪个订单使用
    # 与订单关联,等订单完成,在回来修改
    user = models.ForeignKey(UserModel,on_delete=models.CASCADE,related_name="to_coupon",db_constraint=False,verbose_name="用户")
    coupon = models.ForeignKey(CouponModel,on_delete=models.CASCADE,related_name="to_user",db_constraint=False,verbose_name="优惠券")
    use_time = models.DateTimeField(null=True,blank=True,verbose_name="使用时间")
    use_status = models.SmallIntegerField(choices=USE_CHOICES,default=0,verbose_name="使用状态")
    order = models.ForeignKey(OrdersModel,on_delete=models.DO_NOTHING,null=True,blank=True,related_name="to_coupon",db_constraint=False,verbose_name="订单")
    class Meta():
        db_table = "fg_coupon_log"
        verbose_name_plural = "优惠券发放与日志管理类"
    def __str__(self):
        return  f"{self.coupon.name}发放和使用日志"
